package cn.edu.demo.controller;

import cn.edu.demo.pojo.*;
import cn.edu.demo.pojo.result.OrderlistResult;
import cn.edu.demo.service.CarService;
import cn.edu.demo.service.DriverService;
import cn.edu.demo.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
//@Controller
public class OrderlistController {

    @Autowired
    OrdersService ordersService;

    @Autowired
    CarService carService;

    @Autowired
    DriverService driverService;

    @RequestMapping("/user/orderlist")
    @ResponseBody
    public ResponseResult history(String uid) {
        List<OrdersWithBLOBs> orderList = ordersService.history(uid);
//        System.out.println(orderList);
//        ObjectMapper objectMapper = new ObjectMapper();
        List<OrderlistResult> orderlistResults = new ArrayList<>();
        if (orderList!=null) {
            for (OrdersWithBLOBs orders : orderList)
                orderlistResults.add(new OrderlistResult(driverService.getDriver(orders.getDriverId()), carService.getCar(orders.getCarId()), orders));
            return new ResponseResult(100, "查询成功", orderlistResults);
        }else {
            return new ResponseResult(101, "无历史记录", null);
        }

//        return null;
    }
}
